/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import model.Auction;
import model.Condition;
import model.Item;

/**
 * This class contains a panel that allows for a AuctionCentral
 * employee or NPO to add an item to an auction.
 * 
 * @author Anthony Rios
 *
 */
public class AddItemGUI extends Observable{
	
	private final JPanel my_item_panel;
	
	private final String my_item_info[] = new String[6];
	
	private final List<JTextField> my_text_field_list = new ArrayList<JTextField>();
	
	private final List<String> my_condition_list = new ArrayList<String>();
	
	private final JTextField my_item_name_text = new JTextField(20);
	
	private final JTextField my_item_quantity = new JTextField(20);
	
	private final JTextField my_minimum_starting_bid_text = new JTextField(20);

	private final JTextField my_donor_text = new JTextField(20);
	
	private final JTextField my_size_text = new JTextField(20);
	
	private final JTextField my_storage_text = new JTextField(20);
		
	private final JTextArea my_comment_section = new JTextArea(10,100);
	
	private final JComboBox my_condition_drop_box = new JComboBox();
		
	private final JButton my_submit_button = new JButton("Submit");
	
	private final JButton my_cancel_button = new JButton("Cancel");
	
	private final Auction my_auction; 
	
	/**
	 * the constructor for this GUI.  Creates an observable panel.  To get the panel
	 * use the getPanel method. The parameter is us to send back to the observer so the
	 * observer knows what auction to place the item into.
	 */
	public AddItemGUI(Auction the_auction_item_added_to) {
		my_item_panel = new JPanel();
		my_auction = the_auction_item_added_to;
		my_item_panel.setName("Add Item to Auction");
		my_item_panel.setLayout(new BorderLayout());
		my_comment_section.setSize(800, 600);
		my_submit_button.addActionListener(new submitButtonListener());
		my_cancel_button.addActionListener(new submitButtonListener());
		createItemInfoArray();
		createTextFieldArray();
		createConditionArray();
		layoutComponents();
		addCondtionsToDropDown();
		//MIN START BID INITIALIZED TO 0, BUT EDITABLE
		my_minimum_starting_bid_text.setText("1.00");
	}
	
	/**
	 * layout all the buttons and text fields in the proper format by using
	 * an assortment of panels.
	 */
	public void layoutComponents() {
		final JPanel spring_layout = new JPanel();
		spring_layout.setLayout(new SpringLayout());
        for (int i = 0; i < my_item_info.length; i++) {
        	JLabel label = new JLabel(my_item_info[i]);
        	spring_layout.add(label);
        	spring_layout.add(my_text_field_list.get(i));
        }
        JLabel label = new JLabel("Condition of item");
        spring_layout.add(label);
        spring_layout.add(my_condition_drop_box);
        JLabel comment_label = new JLabel("Comments");
        spring_layout.add(comment_label);
        spring_layout.add(my_comment_section);
        SpringUtilities.makeCompactGrid(spring_layout,
                8, 2,        //rows, cols
                6, 6,        //initX, initY
                6, 6);       //xPad, yPad
        my_item_panel.add(spring_layout, BorderLayout.CENTER);
        JPanel south_panel = new JPanel(new FlowLayout());
        south_panel.add(my_cancel_button);
        south_panel.add(my_submit_button);
        my_item_panel.add(south_panel, BorderLayout.SOUTH);
	}

	public JPanel getPanel() {
		return my_item_panel;
	}
	
	/**
	 * displays an error if any of the text fields are empty.
	 */
	public void displayError()
    {
        JOptionPane.showMessageDialog(null, "One or more text fields are empty");
    }
	
	/**
	 * adds the conditions to the drop down box.
	 */
	private void addCondtionsToDropDown() {
		for (int i = 0; i < my_condition_list.size(); i++) {
			my_condition_drop_box.addItem(my_condition_list.get(i));
		}
		
	}

	/**
	 * creates an array of the Strings that represent the conditions.
	 */
	private void createConditionArray() {
		my_condition_list.add("ACCEPTABLE");
		my_condition_list.add("BRAND NEW");
		my_condition_list.add("GOOD");
		my_condition_list.add("LIKE NEW");
		my_condition_list.add("VERY_GOOD");
	}

	/**
	 * creates an array of the text fields.
	 */
	private void createTextFieldArray() {
		my_text_field_list.add(my_item_name_text);
		my_text_field_list.add(my_item_quantity);
		my_text_field_list.add(my_minimum_starting_bid_text);
		my_text_field_list.add(my_donor_text);
		my_text_field_list.add(my_size_text);
		my_text_field_list.add(my_storage_text);
	}

	/**
	 * helper method to create the array that holds the strings that explain
	 * what should be entered in each text field.
	 */
	private void createItemInfoArray() {
		my_item_info [0] = "Item Name";
		my_item_info [1] = "Item Quantity";
		my_item_info [2] = "Minimum Starting Bid";
		my_item_info [3] = "Donor";
		my_item_info [4] = "Size";
		my_item_info [5] = "Storage";
	}
	
	/**
     * Action listeners for the submit and cancel button.
     * 
     * @author Anthony Rios
     * @version 05-28-12
     */
    protected class submitButtonListener implements ActionListener
    {
    	
     /**
      * checks to see if the_event is the cancel button or submit button.
      * If its the submit button the fields are all check to make sure they are not
      * empty.  If they are not empty a new Item is created and sent to the observer.
      * If the cancel button is clicked a string "cancel" is simply sent to the observer.
      */
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
    	  if (the_event.getActionCommand().equals(my_submit_button.getActionCommand())) {
    		  boolean empty = false;
    		  for (int i = 0; i < my_text_field_list.size(); i++) {
    			  String field_text = my_text_field_list.get(i).getText();
    			  if (field_text.isEmpty() && !(i == 3 || i == 5)) {
    				  displayError();
    				  empty = true;
    				  break;
    			  }
    		  }
    		  if (!empty) {
    	    	  int item_id = Item.next_item_id;
    	    	  Item.next_item_id++;
    			  String item_name = my_item_name_text.getText();
    			  int quantity = Integer.parseInt(my_item_quantity.getText());
    			  int min_start_bid = Integer.parseInt(my_minimum_starting_bid_text.getText().replaceAll("[.]", ""));
    			  String donor = my_donor_text.getText();
    			  int size = Integer.parseInt(my_size_text.getText());
    			  String storage = my_storage_text.getText();
    			  Condition condition = figureOutCondition();
    			  String comment = my_comment_section.getText();
    			  System.out.print(comment);
    			  Item new_item = new Item(item_id, item_name, quantity, 
    					  min_start_bid, donor, size, storage, condition, comment);
    			  setChanged();
    			  notifyObservers(my_auction);
    			  setChanged();
    	       	  notifyObservers(new_item);
    		  }
    	  }
    	  else if (the_event.getActionCommand().equals(my_cancel_button.getActionCommand())) {
    		  setChanged();
			  notifyObservers(AddAuctionGUI.CANCEL);
    	  }
       }

    /**
     * A helper method used to figure out the selected condition from the drop box.
     * The condition is returned to be used to create a new item.
     */
	private Condition figureOutCondition() {
		  Condition the_condition;
		if (my_condition_drop_box.getSelectedItem().equals("ACCEPTABLE")) {
			  the_condition = Condition.ACCEPTABLE;
		  }
		  else if (my_condition_drop_box.getSelectedItem().equals("BRAND NEW")) {
			  the_condition = Condition.BRAND_NEW;
		  }
		  else if (my_condition_drop_box.getSelectedItem().equals("GOOD")) {
			  the_condition = Condition.GOOD;
		  }
		  else if (my_condition_drop_box.getSelectedItem().equals("LIKE NEW")) {
			  the_condition = Condition.LIKE_NEW;
		  }
		  else {
			  the_condition = Condition.VERY_GOOD;
		  }
		return the_condition;
	}
     }
}
